<!--
 * @Description: 
 * @Author: zhanyinjia
 * @Date: 2023-08-09 14:43:55
 * @LastEditors: zhanyinjia
 * @LastEditTime: 2024-06-12 11:29:44
-->
<template>
  <div class="wrap flex f-x-s">
    <el-tag
      class="tag"
      v-for="(tag, index) in cutData(tags)"
      :key="index"
      size="small"
      closable
      @close="close(index)"
    >{{ tag }}</el-tag>
    <el-tooltip
      class="item"
      effect="dark"
      :content="moreTagsStr"
      placement="bottom"
    >
      <div>
        <el-tag
          v-if="tags.length > max"
          class="tag"
          size="small"
        >+{{ tags.length - max }}</el-tag>
      </div>
    </el-tooltip>
    <el-input
      class="auto-size flex-auto"
      v-model.trim="word"
      placeholder="请输入标签"
      @keydown.enter.native="add"
    ></el-input>
  </div>
</template>

<script>
export default {
  name: 'InputTags',
  data() {
    return {
      tags: [],
      word: '',
      max: 3
    }
  },
  computed: {
    moreTagsStr() {
      const lastTags = this.tags.slice(this.max)
      return lastTags.join('、')
    }
  },
  methods: {
    cutData(list) {
      return list.slice(0, this.max)
    },
    add() {
      if (this.tags.includes(this.word) || !this.word) return
      this.tags.push(this.word)
      this.word = ''
    },
    close(index) {
      this.tags.splice(index, 1)
    }
  }
}
</script>

<style lang="less" scoped>
.wrap {
  border: 1px solid #ccc;
  padding: 5px;
  border-radius: 4px;
}

.tag {
  margin: 5px 8px 0 0;
}

.auto-size {
  width: auto;
}

/deep/ .el-input__inner {
  border: 1px solid transparent;
}
</style>